import "./common/reset.css";
import "./common/margin_padding.css";
import "element-ui/lib/theme-chalk/index.css";
// 引入自定义指令文件
import "./common/directive.js";
import "babel-polyfill";

// 视频
import "video.js/dist/video-js.css";
// import 'default-passive-events';
import Vue from "vue";
import App from "./App";
import router from "./router";
import store from "./store";
import VueClipboard from "vue-clipboard2";
import qs from "qs";
import axios from "axios";
import echarts from "echarts";
import wordcloud from "echarts-wordcloud"; //词云
import china from "echarts/map/json/china.json"; // 中国地图
import ElementUI from "element-ui";
import BaiduMap from "vue-baidu-map"; //百度地图
Vue.use(BaiduMap, {
  ak: "Wiu5n608FZhvIg0LUvZjqOO3eqbHubVq"
});
import VueLazyload from "vue-lazyload"; //图片懒加载
Vue.use(VueLazyload);

// 点击图片放大
// import preview from 'vue-photo-preview';
// import 'vue-photo-preview/dist/skin.css';
// Vue.use(preview);
// 引入 vue-swiper
// import VueAwesomeSwiper from 'vue-awesome-swiper'
// import 'swiper/css/swiper.css'
// Vue.use(VueAwesomeSwiper)


let event_bus = new Vue();
Object.defineProperties(Vue.prototype, {
  $bus: {
    get() {
      return event_bus;
    }
  }
});
//复制
Vue.use(VueClipboard);
Vue.prototype.$qs = qs;

Vue.prototype.$echarts = echarts;
Vue.prototype.$wordcloud = wordcloud; // 词云
echarts.registerMap("china", china); //地图
Vue.use(ElementUI, {
  zIndex: 300
});

Vue.config.productionTip = false;
// 二维码
import VueQr from "vue-qr";
Vue.use(VueQr);

import {
  debounce,
  format_num,
  initTime,
  timestamp,
  format_time,
  format_timehm,
  formatDate,
  formatDateNew,  //毫秒值转换成 年月日时分秒  调用方式 YY-MM-DD hh:mm:ss 或者 YYYY-MM-DD hh:mm:ss  如果值获取时间 则直接hh:mm:ss 或  mm:ss
  formatRate,
  unique,
  proFrom,
  trimStr,
  formateSeconds,
  urlPms,
  getStr,
  clearLocal
} from "./common/utils.js";
import {
  fixedThead
} from "./common/common.js";
Vue.prototype.urlPms = urlPms;
Vue.prototype.format_time = format_time;
Vue.prototype.format_timehm = format_timehm;
Vue.prototype.formateSeconds = formateSeconds;
Vue.prototype.debounce = debounce;
Vue.prototype.format_num = format_num;
Vue.prototype.initTime = initTime;
Vue.prototype.timestamp = timestamp;
Vue.prototype.formatDate = formatDate;
Vue.prototype.formatDateNew = formatDateNew;
Vue.prototype.formatRate = formatRate;
Vue.prototype.unique = unique;
Vue.prototype.proFrom = proFrom;
Vue.prototype.trimStr = trimStr;
Vue.prototype.getStr = getStr;
Vue.prototype.fixedThead = fixedThead;
Vue.prototype.$clearLocal = clearLocal;

import infiniteScroll from "vue-infinite-scroll";
Vue.use(infiniteScroll);

import global from "./common/global.js";
Vue.prototype.$global = global;

// axios配置
Vue.prototype.$axios = axios;
axios.defaults.baseURL = process.env.BASE_URL;
axios.interceptors.request.use(
  config => {
    // if (config.method === "post") {
    //   config.data = qs.stringify(config.data);
    // }
    if (localStorage.getItem("user_id")) {
      config.headers.AccountId = localStorage.getItem("user_id");
    }
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);
// 响应拦截器 对1006 请登录进行处理
axios.interceptors.response.use(
  function(response) {
    let redirectUrl = location.href;
    if (response.data.code == 1010) { //被挤掉，第一次调用接口，返回的1010
      // if(location.pathname == "/"){
      //   ElementUI.Message({
      //     showClose: true,
      //     message:
      //       "111您的账号登录设备数已超出会员设备限制，请升级会员或重新登录使用",
      //     customClass: "includeMessage",
      //     offset: 154
      //   });
      // }else{
      //   localStorage.setItem("isLoginRoot",1);
      //   location.href = "/";
      // }

      if(location.pathname != "/"){
        localStorage.setItem("isLoginRoot",1);
        location.href = '/?redirectUrl='+encodeURIComponent(redirectUrl);
      }

      if (localStorage.getItem("user_id")) {
        clearLocal();
      }
    } else if (response.data.code == 1006) {
      if(localStorage.getItem("isLoginRoot")){
        if(location.pathname == "/"){
          localStorage.removeItem("isLoginRoot");
          ElementUI.Message({
            showClose: false,
            message:
              "您的账号登录设备数已超出会员设备限制，请升级会员或重新登录使用",
            customClass: "includeMessage",
            offset: 154
          });
        }
      }
      // if (localStorage.getItem("user_id")) {
      clearLocal();
      if (location.pathname != "/") {
        location.href = '/?redirectUrl='+encodeURIComponent(redirectUrl);
      }
      // }
      return response;
    }else{
      return response;
    }
  },
  function(error) {
    // 对响应错误做点什么
    return Promise.reject(error);
  }
);

//引入封装通用组件库
import components from "./component";
Vue.use(components);
new Vue({
  el: "#app",
  router,
  store,
  components: {
    App
  },
  template: "<App/>"
});
